﻿using GalaSoft.MvvmLight.Messaging;
using KINLO.DRV.Drv;
using log4net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace KINLO.DRV.Machine
{
    public class OmronInjectionMachine
    {
        #region 字段

        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(OmronInjectionMachine));
        OmronPlcSocketDrv PLC;
        public System.Timers.Timer timer;
        string Terminator = "*\r";
        public string Name;
        public string IP;
        public int Port;
        object locker = new object();
        #endregion

        #region 属性 PLC TO PC 
        Int16 d1200;
        /// <summary>
        /// 启动前扫码
        /// </summary>
        public Int16 D1200
        {
            get { return d1200; }
            set
            {
                if (d1200 != value)
                {
                    d1200 = value;
                    if (d1200 == 1)
                    {
                        Thread.Sleep(200);
                        log.Info(string.Format("收到 D1200 值：{0} 启动前扫描命令", D1200));
                        Messenger.Default.Send("", "FStartScan");
                    }
                }
            }
        }
        /// <summary>
        /// 注液前称重
        /// </summary>
        Int16 d1201;
        public Int16 D1201
        {
            get { return d1201; }
            set
            {
                if (d1201 != value)
                {
                    d1201 = value;
                    if (d1201 == 1)
                    {
                        Thread.Sleep(200);

                        log.Info(string.Format("收到 D1201 值：{0} 注液前称重命令", D1201));
                        Messenger.Default.Send("", "FInjectionWeight");
                    }
                }
            }
        }
        /// <summary>
        /// 启动后扫描
        /// </summary>
        Int16 d1202;
        /// <summary>
        /// 启动后扫描
        /// </summary>
        public Int16 D1202
        {
            get { return d1202; }
            set
            {
                if (d1202 != value)
                {
                    d1202 = value;
                    if (d1202 == 1)
                    {
                        Thread.Sleep(200);
                        log.Info(string.Format("收到D1202值：{0} 启动后扫描命令", D1202));
                        Messenger.Default.Send("", "BStartScan");
                    }
                }
            }
        }
        /// <summary>
        ///  注液后称重
        /// </summary>
        Int16 d1203;
        /// <summary>
        /// 注液后称重
        /// </summary>
        public Int16 D1203
        {
            get { return d1203; }
            set
            {
                if (d1203 != value)
                {
                    d1203 = value;
                    if (d1203 == 1)
                    {
                        Thread.Sleep(200);
                        log.Info(string.Format("收到 D1203 值：{0} 启动后扫描命令", D1203));
                        Messenger.Default.Send("", "BInjectionWeight");
                    }
                }
            }
        }
        /// <summary>
        /// 补液称重
        /// </summary>
        Int16 d1205;
        /// <summary>
        /// 补液称重
        /// </summary>
        public Int16 D1205
        {
            get { return d1205; }
            set
            {
                if (d1205 != value)
                {
                    d1205 = value;
                    if (d1205 == 1)
                    {
                        Thread.Sleep(200);
                        log.Info(string.Format("收到 D1205 值：{0} 启动后扫描命令", D1205));
                        Messenger.Default.Send("", "RepairLiquidWeight");
                    }
                }
            }
        }
        /// <summary>
        ///  注液前称重清零
        /// </summary>
        Int16 d1206;
        /// <summary>
        /// 注液前称重清零
        /// </summary>
        public Int16 D1206
        {
            get { return d1206; }
            set
            {
                if (d1206 != value)
                {
                    d1206 = value;
                    if (d1206 == 1)
                    {
                        Thread.Sleep(200);
                        log.Info(string.Format("收到 D1206 值：{0} 注液前称重清零", D1205));
                        Messenger.Default.Send("", "BInjectionWeightClear");
                    }
                }
            }
        }
        /// <summary>
        /// 注液后称重清零
        /// </summary>
        Int16 d1207;
        /// <summary>
        /// 注液后称重清零
        /// </summary>
        public Int16 D1207
        {
            get { return d1207; }
            set
            {
                if (d1207 != value)
                {
                    d1207 = value;
                    if (d1207 == 1)
                    {
                        Thread.Sleep(200);
                        log.Info(string.Format("收到 D1208 值：{0} 注液后称重清零", D1208));
                        Messenger.Default.Send("", "AInjectionWeightClear");
                    }
                }
            }
        }
        /// <summary>
        /// 补液称重清零
        /// </summary>
        Int16 d1208;
        /// <summary>
        /// 补液称重清零
        /// </summary>
        public Int16 D1208
        {
            get { return d1208; }
            set
            {
                if (d1208 != value)
                {
                    d1208 = value;
                    if (d1208 == 1)
                    {
                        Thread.Sleep(200);
                        log.Info(string.Format("收到 D1208 值：{0} 补液称重清零", D1208));
                        Messenger.Default.Send("", "RInjectionWeightClear");
                    }
                }
            }
        }
        /// <summary>
        /// 前扫码托架号
        /// </summary>
        Int16 d1220;
        /// <summary>
        /// 前扫码托架号
        /// </summary>
        public Int16 D1220
        {
            get { return d1220; }
            set
            {
                if (d1220 != value)
                {
                    d1220 = value;
                }
            }
        }
        /// <summary>
        /// 注液前托架号
        /// </summary>
        Int16 d1221;
        /// <summary>
        /// 注液前托架号
        /// </summary>
        public Int16 D1221
        {
            get { return d1221; }
            set
            {
                if (d1221 != value)
                {
                    log.Info(string.Format("注液前托架号:{0}", D1221));
                    d1221 = value;
                }
            }
        }
        /// <summary>
        /// 后扫码托架号
        /// </summary>
        Int16 d1222;
        public Int16 D1222
        {
            get { return d1222; }
            set
            {
                if (d1222 != value)
                {
                    d1222 = value;
                }
            }
        }
        /// <summary>
        /// 注液后托架号
        /// </summary>
        Int16 d1223;
        /// <summary>
        /// 注液后托架号
        /// </summary>
        public Int16 D1223
        {
            get { return d1223; }
            set
            {
                if (d1223 != value)
                {
                    d1223 = value;
                }
            }
        }
        /// <summary>
        /// 补液称重托架号
        /// </summary>
        Int16 d1225;
        /// <summary>
        /// 补液称重托架号
        /// </summary>
        public Int16 D1225
        {
            get { return d1225; }
            set
            {
                if (d1225 != value)
                {
                    d1225 = value;
                }
            }
        }
        #endregion

        #region 方法

        public OmronInjectionMachine(string ip, int port, int interval, string name)
        {
            IP = ip;
            Port = port;
            Name = name;
            timer = new System.Timers.Timer(interval);
            timer.Elapsed += Timer_Elapsed;
            PLC = new OmronPlcSocketDrv(ip, port);
        }
        int inTimer = 0;
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        { 
            if (Interlocked.Exchange(ref inTimer, 1) == 0)
            {
                if (PLC.Connected != true)
                    PLC.Open();
                if (PLC.Connected)
                {
                    string result2 = "";
                    if (Monitor.TryEnter(locker, 2000))
                    {
                        try
                        {
                            var str2 = "RD12000014";
                            result2 = Write(str2);
                           // log.Info(string.Format("result2:{0}", result2));
                            ReadCmdExplain(result2);
                        }
                        catch (Exception ex)
                        {
                            log.Error(ex.Message);
                        }
                        finally
                        {
                            Monitor.Exit(locker);
                        }
                    }

                    //if (Monitor.TryEnter(locker, 2000))
                    //{
                    //    try
                    //    {
                    //        var str2 = "RD12200006";
                    //        result2 = Write(str2);
                    //        log.Info(string.Format("result2:{0}", result2));
                    //        ReadCmdExplain1(result2);
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        log.Error(ex.Message);
                    //    }
                    //    finally
                    //    {
                    //        Monitor.Exit(locker);
                    //    }
                    //}

                    //string result1 = "";
                    //if (Monitor.TryEnter(locker, 2000))
                    //{
                    //    try
                    //    {
                    //        var str1 = "RD12000009";
                    //        result1 = Write(str1);
                    //        log.Info(string.Format("result1:{0}", result1));
                    //        ReadCmdExplain2(result1);
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        log.Error(ex.Message);
                    //    }
                    //    finally
                    //    {
                    //        Monitor.Exit(locker);
                    //    }
                    //}
                }
                else
                {
                    var errorMs = string.Format("注液机:{0}:{1} 已离线", IP, Port);
                    log.Error(errorMs);
                }
                Interlocked.Exchange(ref inTimer, 0);
            }
        }
        string Write(string cmd)
        {
            var sendCmd = PackCmd(cmd);
            PLC.Write(sendCmd);
            var result = PLC.Read();
            return result;
        }
        string WriteCmdExplain(string cmd)
        {
            try
            {
                var errorCode = cmd.Substring(5, 2);
                if (errorCode != "00")
                {
                    var header = cmd.Substring(3, 2);
                    var fcs = cmd.Substring(cmd.Length - 3, 2);
                    var checkFcs = FCS(cmd.Substring(0, cmd.Length - 3));
                    var errorMsg = ErrorCodeMsg(errorCode);
                    var message= string.Format("解析读操作发生了错误 错误码:{0} 错误信息:{1}", errorCode, errorMsg);
                    log.Error(message);
                    return message;
                }
                else
                {
                    return " 完成";
                }

            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0},{1},{2}", "OmronInjectionMachine", "WriteCmdExplain", ex));
                return "发生错误:" + ex.Message;
            }

        }

        void ReadCmdExplain(string cmd)
        {
            try
            {
                if (cmd.Contains("@00WD"))
                    return;
                if (cmd.Length < 7)
                    return;
                cmd = cmd.Replace("*", "").Split('\r')[0];
                var header = cmd.Substring(3, 2);
                var errorCode = cmd.Substring(5, 2);
                var fcs = cmd.Substring(cmd.Length - 2, 2);
                var checkFcs = FCS(cmd.Substring(0, cmd.Length - 2));
                if (checkFcs.ToUpper() != fcs.ToUpper())
                {
                    log.Error(string.Format("FCS校验错误 {0}", cmd));
                    return;
                }
                if (errorCode == "00")
                {
                    var body = cmd.Substring(7, cmd.Length - 2 - 7); 
                    int count = body.Length / 4;
                    var Values2 = new Int16[count];
                    for (int i = 0; i < count; i++)
                    {
                        var hexVal = body.Substring(i * 4, 4);
                        var decVal = HexToDecimal(hexVal);
                        Values2[i] = decVal;
                    }

                    D1220 = Values2[9];
                    D1221 = Values2[10];
                    D1222 = Values2[11];
                    D1223 = Values2[12];
                    D1225 = Values2[13];
                    D1200 = Values2[0];
                    D1201 = Values2[1];
                    D1202 = Values2[2];
                    D1203 = Values2[3];
                   // D1204 = Values2[4];
                    D1205 = Values2[5];
                    D1206 = Values2[6];
                    D1207 = Values2[7];
                    D1208 = Values2[8];
                    //log.Info(string.Format("-----D1220:{0}", D1220));
                    //log.Info(string.Format("--D1221:{0}", D1221));
                    //log.Info(string.Format("-----D1223:{0}", D1223));
                    //log.Info(string.Format("--D1225:{0}", D1225));



                    //D1220 = Values2[0];
                    //D1221 = Values2[1];
                    //log.Info(string.Format("-----D1220:{0}", D1220));
                    //log.Info(string.Format("--D1221:{0}", D1221));
                    //D1222 = Values2[2];
                    //D1223 = Values2[3];
                    //D1225 = Values2[5];
                }
                else
                {
                    var errorMsg = ErrorCodeMsg(errorCode);
                    log.Error(string.Format("读 {0} 发生了错误 错误码:{1} 错误信息:{2}", header, errorCode, errorMsg));

                    //错误处理  Messenger.Default.Send("", "erroHandle");
                }
            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0},{1},{2}", "OmronInjectionMachine", "ReadCmdExplain", ex));
            }

        }

        void ReadCmdExplain1(string cmd)
        {
            try
            {
                if (cmd.Length < 7)
                    return;
                cmd = cmd.Replace("*", "").Split('\r')[0];
                var header = cmd.Substring(3, 2);
                var errorCode = cmd.Substring(5, 2);
                var fcs = cmd.Substring(cmd.Length - 2, 2);
                var checkFcs = FCS(cmd.Substring(0, cmd.Length - 2));
                if (checkFcs.ToUpper() != fcs.ToUpper())
                {
                    log.Error(string.Format("FCS校验错误 {0}", cmd));
                    return;
                }
                if (errorCode == "00")
                {
                    var body = cmd.Substring(7, cmd.Length - 2 - 7);
                     
                        int count = body.Length / 4;
                        var Values2 = new Int16[count];
                        for (int i = 0; i < count; i++)
                        {
                            var hexVal = body.Substring(i * 4, 4);
                            var decVal = HexToDecimal(hexVal);
                            Values2[i] = decVal;
                        }
                        D1220 = Values2[0];
                        D1221 = Values2[1];
                        log.Info(string.Format("-----D1220:{0}", D1220));
                        log.Info(string.Format("--D1221:{0}", D1221));
                        D1222 = Values2[2];
                        D1223 = Values2[3];
                        D1225 = Values2[5]; 
                }
                else
                {
                    var errorMsg = ErrorCodeMsg(errorCode);
                    log.Error(string.Format("读 {0} 发生了错误 错误码:{1} 错误信息:{2}", header, errorCode, errorMsg));

                    //错误处理  Messenger.Default.Send("", "erroHandle");
                }
            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0},{1},{2}", "OmronInjectionMachine", "ReadCmdExplain", ex));
            }

        }

        void ReadCmdExplain2(string cmd)
        {
            try
            {
                if (cmd.Length < 7)
                    return;
                cmd = cmd.Replace("*", "").Split('\r')[0];
                var header = cmd.Substring(3, 2);
                var errorCode = cmd.Substring(5, 2);
                var fcs = cmd.Substring(cmd.Length - 2, 2);
                var checkFcs = FCS(cmd.Substring(0, cmd.Length - 2));
                if (checkFcs.ToUpper() != fcs.ToUpper())
                {
                    log.Error(string.Format("FCS校验错误 {0}", cmd));
                    return;
                }
                if (errorCode == "00")
                {
                    var body = cmd.Substring(7, cmd.Length - 2 - 7);
                        int count = body.Length / 4;
                        var Values1 = new Int16[count];
                        for (int i = 0; i < count; i++)
                        {
                            var hexVal = body.Substring(i * 4, 4);
                            var decVal = HexToDecimal(hexVal);
                            Values1[i] = decVal;
                        }
                        D1200 = Values1[0];
                        D1201 = Values1[1];
                        D1202 = Values1[2];
                        D1203 = Values1[3];
                        D1205 = Values1[5];
                        D1206 = Values1[6];
                        D1207 = Values1[7];
                        D1208 = Values1[8]; 
                }
                else
                {
                    var errorMsg = ErrorCodeMsg(errorCode);
                    log.Error(string.Format("读 {0} 发生了错误 错误码:{1} 错误信息:{2}", header, errorCode, errorMsg));

                    //错误处理  Messenger.Default.Send("", "erroHandle");
                }
            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0},{1},{2}", "OmronInjectionMachine", "ReadCmdExplain", ex));
            }

        }
        void ReadCmdExplain(string cmd, int type)
        {
            try
            {
                if (cmd.Length < 7)
                    return;
                cmd = cmd.Replace("*", "").Split('\r')[0];
                var header = cmd.Substring(3, 2);
                var errorCode = cmd.Substring(5, 2);
                var fcs = cmd.Substring(cmd.Length - 2, 2);
                var checkFcs = FCS(cmd.Substring(0, cmd.Length - 2));
                if (checkFcs.ToUpper() != fcs.ToUpper())
                {
                    log.Error(string.Format("FCS校验错误 {0}", cmd)); 
                    return;
                }
                if (errorCode == "00")
                {
                    var body = cmd.Substring(7, cmd.Length - 2 - 7);
                    if (type == 1)
                    {
                        int count = body.Length / 4;
                        var Values1 = new Int16[count];
                        for (int i = 0; i < count; i++)
                        {
                            var hexVal = body.Substring(i * 4, 4);
                            var decVal = HexToDecimal(hexVal);
                            Values1[i] = decVal;
                        }
                        D1200 = Values1[0];
                        D1201 = Values1[1];
                        D1202 = Values1[2];
                        D1203 = Values1[3];
                        D1205 = Values1[5];
                        D1206 = Values1[6];
                        D1207 = Values1[7];
                        D1208 = Values1[8];
                    }
                    if (type == 2)
                    {
                        int count = body.Length / 4;
                        var Values2 = new Int16[count];
                        for (int i = 0; i < count; i++)
                        {
                            var hexVal = body.Substring(i * 4, 4);
                            var decVal = HexToDecimal(hexVal);
                            Values2[i] = decVal;
                        }
                        D1220 = Values2[0];
                        D1221 = Values2[1];
                        log.Info(string.Format("-----D1220:{0}", D1220));
                        log.Info(string.Format("--D1221:{0}", D1221));
                        D1222 = Values2[2];
                        D1223 = Values2[3];
                        D1225 = Values2[5];
                    }
                }
                else
                {
                    var errorMsg = ErrorCodeMsg(errorCode);
                    log.Error(string.Format("读 {0} 发生了错误 错误码:{1} 错误信息:{2}", header, errorCode, errorMsg));

                    //错误处理  Messenger.Default.Send("", "erroHandle");
                }
            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0},{1},{2}", "OmronInjectionMachine", "ReadCmdExplain", ex));
            }
        }

        /// <summary>
        /// 前扫码完成
        /// </summary>
        /// <param name="s"></param> 
        public void WriteFScanFin(bool s)
        {
            if (Monitor.TryEnter(locker, 3000))
            {
                try
                {
                    string cmd = "";
                    if (s)
                    {
                        cmd = "WD12400001";
                    }
                    else
                    {
                        cmd = "WD12400000";
                    }
                    var result = Write(cmd);
                    var msg = WriteCmdExplain(result);
                    log.Info(string.Format("写 前扫码完成 {0} ", msg));
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
                finally
                {
                    Monitor.Exit(locker);
                }
            }

        }

        /// <summary>
        /// 注液前称重完成
        /// </summary>
        /// <param name="s"></param> 
        public void WriteFInjectionWeightFin()
        {

            if (Monitor.TryEnter(locker, 3000))
            {
                try
                {
                    string cmd = "WD12410001";
                    var result = Write(cmd);
                    var msg = WriteCmdExplain(result);
                    log.Info(string.Format("写 注液前称重完成 值：{0} ", msg));
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
                finally
                {
                    Monitor.Exit(locker);
                }
            }
        }

        /// <summary>
        /// 注液后称重完成
        /// </summary>
        /// <param name="s"></param> 
        public void WriteBInjectionWeightFin()
        {
            if (Monitor.TryEnter(locker, 3000))
            {
                try
                {
                    string cmd = "WD12500001";

                    var result = Write(cmd);
                    var msg = WriteCmdExplain(result);
                    log.Info(string.Format("写 注液后称重完成 值：{0}", msg));
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
                finally
                {
                    Monitor.Exit(locker);
                }
            }
        }

        /// <summary>
        /// 注液后称重合格标志 OK
        /// </summary>
        /// <param name="s"></param> 
        public void WriteBInjectionWeightOK(bool s)
        {
            if (Monitor.TryEnter(locker, 3000))
            {
                try
                {
                    string cmd = "";
                    if (s)
                    {
                        cmd = "WD12510001";
                    }
                    else
                    {
                        cmd = "WD12510000";
                    }
                    var result = Write(cmd);
                    var msg = WriteCmdExplain(result);
                    log.Info(string.Format("写 注液后称重合格标志 值：{0} {1}", s, msg));
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
                finally
                {
                    Monitor.Exit(locker);
                }
            }
        }

        /// <summary>
        /// 后称重低于最小值 NG
        /// </summary>
        /// <param name="s"></param> 
        public void WriteBWeightLowVal()
        {
            if (Monitor.TryEnter(locker, 3000))
            {
                try
                {
                    string cmd = "WD12520001";
                    var result = Write(cmd);
                    var msg = WriteCmdExplain(result);
                    log.Info(string.Format("写 后称重低于最小值 {0} ", msg));
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
                finally
                {
                    Monitor.Exit(locker);
                }
            }
        }

        /// <summary>
        /// 后称重补液值
        /// </summary>
        /// <param name="s"></param> 
        public void WriteBWeightFluidInfusionVal(int s)
        {
            if (Monitor.TryEnter(locker, 3000))
            {
                try
                {
                    var cmd = string.Format("{0}{1}", "WD1253", s.ToString("X4"));
                    var result = Write(cmd);
                    var msg = WriteCmdExplain(result);
                    log.Info(string.Format("写 后称重补液值 值：{0} {1}", s, msg));
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
                finally
                {
                    Monitor.Exit(locker);
                }
            }
        }

        /// <summary>
        /// 后称重超重值
        /// </summary>
        /// <param name="s"></param> 
        public void WriteAWeightOverVal(int s)
        {
            if (Monitor.TryEnter(locker, 3000))
            {
                try
                {
                    var cmd = string.Format("{0}{1}", "WD1254", s.ToString("X4"));
                    var result = Write(cmd);
                    var msg = WriteCmdExplain(result);
                    log.Info(string.Format("写 后称重超重值 值：{0} {1}", s, msg));
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
                finally
                {
                    Monitor.Exit(locker);
                }
            }
        }
        /// <summary>
        /// 补液称重完成
        /// </summary>
        /// <param name="s"></param> 
        public void WriteRepairWeightFin()
        {
            if (Monitor.TryEnter(locker, 3000))
            {
                try
                {
                    string cmd = "WD12600001";

                    var result = Write(cmd);
                    var msg = WriteCmdExplain(result);
                    log.Info(string.Format("写 补液称重完成  {0}", msg));
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
                finally
                {
                    Monitor.Exit(locker);
                }
            }

        }
        /// <summary>
        /// 补液称重合格标志
        /// </summary>
        /// <param name="s"></param> 
        public void WriteRepairWeightQualified(bool s)
        {
            if (Monitor.TryEnter(locker, 3000))
            {
                try
                {
                    var cmd = "";
                    if (s)
                    {
                        cmd = "WD12610001";
                    }
                    else
                    {
                        cmd = "WD12610000";
                    }
                    var result = Write(cmd);
                    var msg = WriteCmdExplain(result);
                    log.Info(string.Format("写 补液称重合格标志 值：{0} {1}", s, msg));
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
                finally
                {
                    Monitor.Exit(locker);
                }
            }
        }

        public Int16 HexToDecimal(string hex)
        {
            var b = Int16.Parse(hex, System.Globalization.NumberStyles.HexNumber);
            return b;
        }
        string ErrorCodeMsg(string code)
        {
            switch (code)
            {
                case "00":
                    return "正常完成";
                case "01":
                    return "不能执行在 RUN 模式";
                case "02":
                    return "不能执行在 监视 模式";
                case "03":
                    return "UM 写保护";
                case "04":
                    return "地址溺出";
                case "13":
                    return "FCS 校验错误";
                case "14":
                    return "格式错误";
                case "15":
                    return "输入数字号码错误";
                case "18":
                    return "帧长度错误";
                case "19":
                    return "不可执行";
                case "20":
                    return "不能创建I/O表";
                case "21":
                    return "不能执行由于CPU单元CPU错误";
                case "A3":
                    return "在传输数据中由于FCS错误而中止";
                case "A4":
                    return "在传输数据中由于格式错误而中止";
                case "A5":
                    return "在传输数据中由于输入的数据号码错误而中止";
                case "A8":
                    return "在传输数据中由于帧长度错误而中止";
                case "0B":
                    return "在编程模式下无法执行";
                default:
                    return "未知错误";
            }
        }
        public string FCS(string data)
        {
            int xorresult = 0;
            string tempfes = "";
            for (int i = 0; i < data.Length; i++)
            {
                xorresult = xorresult ^ Convert.ToInt32(data[i]);
            }
            tempfes = Convert.ToString(xorresult, 16);
            if (tempfes.Length == 1)
            {
                tempfes = "0" + tempfes;
            }
            return tempfes;
        }
        public string PackCmd(string cmd)
        {
            cmd = string.Format("@00{0}", cmd);
            var fcs = FCS(cmd).ToUpper();
            cmd = string.Format("{0}{1}{2}", cmd, fcs, Terminator);
            return cmd;
        }
        public void Start()
        {
            log.Info("start");
            PLC.Open();
            timer.Enabled = true;
        }
        public void Stop()
        {
            log.Info("Stop");
            timer.Enabled = false;
            PLC.Disconnect();
        }
        public bool Connected
        {
            get { return PLC.Connected; }
        }

        #endregion 
    }
}
